package com.example.demo.controller;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.mbeans.BaseCatalinaMBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.dto.ResultJson;
import com.example.demo.entity.User;
import com.example.demo.mapper.User_Mapper;
import com.example.demo.utils.DateUtils;
import com.example.demo.utils.MD5Utils;

@RestController
@RequestMapping("User")
public class User_Controller extends BasicController<User> {
	@Autowired
	User_Mapper mapper;

	@RequestMapping("login")
	public ResultJson login(User u,HttpSession s) {
		User nu =mapper.login(u);
		if(nu==null) return new ResultJson(-1,"用户不存在");
		if(!nu.getPass().equals(MD5Utils.MD5(u.getPass())))
			return new ResultJson(-2,"用户密码错误");
		s.setMaxInactiveInterval(600);
		s.setAttribute("user", nu);
		return new ResultJson(1);
	}
	
	@RequestMapping("outlogin")
	public void outlogin(HttpSession s,HttpServletResponse resp)throws Exception{
		s.removeAttribute("user");
		resp.sendRedirect("/login.html");
	}
	
	@Override
	public ResultJson insert(User u) {
		u.setPass(MD5Utils.MD5("123"));
		u.setCreatedate(DateUtils.getNowTime());
		return super.insert(u);
	}
}
